﻿using NetSystem;
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace ResourceSystem
{
    public class AssetBundleStream : IResourceReader, IResourceWriter,IDisposable
    {
        public float LoadProgress { get; private set; }

        public object Content { get; private set; }

        public AssetBundle Bundle { get; private set; }

        public byte[] Bytes { get; private set; }

        public Action OnLoadFinish;

        IEnumerator LoadResouce(string resPath, int version)
        {
            while (!Caching.ready)
                yield return null;
            DebugUtils.LogError(resPath);
            
            using (UnityWebRequest uwr = UnityWebRequest.Get(resPath))
            {
                
                yield return uwr.SendWebRequest();
                if (uwr.error != null)
                    throw new Exception("www download had an error" + uwr.error);
                LoadProgress = uwr.downloadProgress;
                if (uwr.isDone)
                {
                    byte[] results = uwr.downloadHandler.data;
                    Bundle = AssetBundle.LoadFromMemory(uwr.downloadHandler.data);
                    Bytes =uwr.downloadHandler.data;

                    FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath + "/" + "test");
                    FileStream fs = fileInfo.Create();

                    //fs.Write(字节数组, 开始位置, 数据长度);
                    fs.Write(uwr.downloadHandler.data, 0, uwr.downloadHandler.data.Length);

                    fs.Flush();     //文件写入存储到硬盘
                    fs.Close();     //关闭文件流对象
                    fs.Dispose();   //销毁文件对象

                    Content = Bundle.LoadAsset(PathUtils.GetFileNameWithoutExtension(resPath));
                    if (OnLoadFinish != null)
                    {
                        OnLoadFinish();
                    }
                }
            }
        }

        public object ReadResouce(string path)
        {
            GameTimeSystemFacade.Inst.StartCoroutine(LoadResouce(path, 0));
            return this;
        }

        public bool WriteResource(IWriteArgs data)
        {
            AssetBundleWriteArgs args = (AssetBundleWriteArgs)data;
            if (args.Bytes==null || (args.Bytes!=null&&args.Bytes.Length<=0))
            {
                GameTimeSystemFacade.Inst.StartCoroutine(SaveAssetBundle(args));
            }
            else
            {

            }
            
            return true;
        }

        /// <summary>
        /// 保存AssetBundle文件到本地
        /// </summary>
        IEnumerator SaveAssetBundle(AssetBundleWriteArgs args)
        {
            while (!Caching.ready)
                yield return null;
            DebugUtils.LogError(args.Path);

            using (UnityWebRequest uwr = UnityWebRequest.Get(args.Path))
            {

                yield return uwr.SendWebRequest();
                if (uwr.error != null)
                    throw new Exception("www download had an error" + uwr.error);
                LoadProgress = uwr.downloadProgress;
                if (uwr.isDone)
                {
                    byte[] results = uwr.downloadHandler.data;
                    Bundle = AssetBundle.LoadFromMemory(uwr.downloadHandler.data);
                    Bytes = uwr.downloadHandler.data;

                    FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath + "/" + Bundle.name);
                    FileStream fs = fileInfo.Create();

                    //fs.Write(字节数组, 开始位置, 数据长度);
                    fs.Write(uwr.downloadHandler.data, 0, uwr.downloadHandler.data.Length);

                    fs.Flush();     //文件写入存储到硬盘
                    fs.Close();     //关闭文件流对象
                    fs.Dispose();   //销毁文件对象
                }
            }
        }

        public void Dispose()
        {
            OnLoadFinish = null;
        }
    }
}
